<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\InsureBalance as InsureBalanceModel;
use think\Db;
class InsureBalance extends BaseController{
    public function __construct(){
        parent::__construct();       
    }
    public function index(){        
        $params = $this->request->param();
        if(!isset($params['date_type']) || $params['date_type']="begdate"){
            $date_type="begdate";
        }else{
            $date_type="insurance_date";
        }
        $balance=0;
        $dates=Db::table('lt_policy')
        ->field("date_format({$date_type},'%Y-%m') date")        
        ->group("date")
        ->select();
        
        $b=Db::table("lt_insure_advance")  
        ->field('sum(amount) amount')
        ->where('payment_date','2020-05')
        ->find();
        $balance = $b['amount'];        
        foreach ($dates as &$v) {
            $data=[];   
            $sub=""; 
            $tdata=[];   
            $tsub=""; 
            $pprice=0;           
            $pfee=0;   
            $tprice=0;           
            $tfee=0;                
            $bdata="";   
            $sub=Db::table('lt_policy')  
            ->field("type,date_format({$date_type},'%Y-%m') date,insure_code,sum(premium) as total")        
            ->where("date_format({$date_type},'%Y-%m')='{$v['date']}' and type=1")
            ->group("date_format({$date_type},'%Y-%m')")    
            ->buildSql();
            $data=Db::table("{$sub} a")  
            ->field("a.*,total*(b.rate/100) as fee")      
            ->join("lt_insure b","a.insure_code=b.insure_code")
            ->find();            
            

            $tsub=Db::table('lt_policy')  
            ->field("type,date_format({$date_type},'%Y-%m') date,insure_code,sum(premium) as total")        
            ->where("date_format({$date_type},'%Y-%m')='{$v['date']}' and type=2")
            ->group("date_format({$date_type},'%Y-%m')")    
            ->buildSql();
            $tdata=Db::table("{$tsub} a")  
            ->field("a.*,total*(b.rate/100) as fee")      
            ->join("lt_insure b","a.insure_code=b.insure_code")
            ->find();

            $pprice = isset($data['total'])?$data['total']:0;
            $pfee = isset($data['fee'])?$data['fee']:0; 

            $tprice = isset($tdata['total'])?$tdata['total']:0;
            $tfee = isset($tdata['fee'])?$tdata['fee']:0; 
                       
            
            $bdata=Db::table("lt_insure_advance")  
            ->field('payment_date,amount')
            ->where('payment_date',$v['date'])
            ->find();            
          
            $v['p_total'] = $pprice;
            $v['t_total'] = $tprice;
            $v['total'] = $pprice + $tprice;
            $v['fee'] = $pfee + $tfee;
            $v['advance'] = $bdata['amount']?$bdata['amount']:0;
            $balance = $bdata['amount']?$balance + $bdata['amount']- $pprice  - $tprice:$balance- $pprice - $tprice;            
            $v['balance'] = $balance;
        }
        
        
        
        return $dates;
    }
   
}
